package com.hrms.repository;

import com.hrms.entity.Employee;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
    Optional<Employee> findByEmployeeNumber(String employeeNumber);
    boolean existsByEmployeeNumber(String employeeNumber);
    
    @Query("SELECT e FROM Employee e WHERE " +
           "(:keyword IS NULL OR e.name LIKE %:keyword% OR e.employeeNumber LIKE %:keyword%) AND " +
           "(:departmentId IS NULL OR e.department.id = :departmentId) AND " +
           "(:positionId IS NULL OR e.position.id = :positionId) AND " +
           "(:status IS NULL OR e.status = :status)")
    Page<Employee> findByFilters(@Param("keyword") String keyword,
                                @Param("departmentId") Long departmentId,
                                @Param("positionId") Long positionId,
                                @Param("status") Employee.Status status,
                                Pageable pageable);
    
    List<Employee> findByStatus(Employee.Status status);
    
    @Query("SELECT COUNT(e) FROM Employee e WHERE e.status = 'ACTIVE'")
    long countActiveEmployees();
}
